package SwingWorkers;

import CommonOperationsClientServer.FilesOperations;
import GUIClient.GUIExtraFunctions;
import GUIClient.GUIOperationInProgress;
import java.io.File;
import java.io.PrintWriter;
import javax.swing.JTextPane;
import javax.swing.SwingWorker;

public class SwingWorkerUploadFile extends SwingWorker<Boolean, Void> {

    GUIOperationInProgress progress = new GUIOperationInProgress();
    File file;
    JTextPane local;
    PrintWriter output;
    JTextPane server;

    public SwingWorkerUploadFile(File f, PrintWriter out, JTextPane localPane, JTextPane serverPane) {
        file = f;
        output = out;
        local = localPane;
        server = serverPane;
    }

    @Override
    protected Boolean doInBackground() throws Exception {
        GUIExtraFunctions.updateTextPane(server, "Solicitud concedida. Enviando OK al cliente");
        GUIExtraFunctions.updateTextPane(local, "Solicitud de Envío aceptada. Enviando fichero...");
        GUIExtraFunctions.updateTextPane(server, "Recibiendo fichero...");
        progress.setVisible(true);
        FilesOperations.sendFile(file, output);
        progress.setVisible(false);
        GUIExtraFunctions.updateTextPane(local, "Envío del fichero finalizado");
        GUIExtraFunctions.updateTextPane(server, "Recepción del fichero finalizada");
        return true;
    }
}
